package com.leather.common.vo;

import com.leather.common.entity.Message;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;

@Data
public class MessageVo implements Serializable {

    private Integer messageType;

    /**
     * 消息标题
     * isNullAble:0
     */
    private String messageTitle;

    /**
     * 消息内容
     * isNullAble:0
     */
    private String messageContent;

    /**
     * 消息跳转的页面路径
     * isNullAble:1
     */
    private String messageUrl;

    /**
     * 发送者id
     */
    private Integer userId;

    public Result validate() {
        if (userId <= 0) {
            return Result.error(CodeMsg.ERROR_USER_ID);
        }
        if (StringUtils.isBlank(messageTitle)) {
            return Result.error(CodeMsg.EMPTY_MESSAGE_TITLE);
        }
        if (StringUtils.isBlank(messageContent)) {
            return Result.error(CodeMsg.EMPTY_MESSAGE_CONTENT);
        }
        if (messageType == 0 && StringUtils.isBlank(messageUrl)) {
            return Result.error(CodeMsg.ERROR_MESSAGE_URL);
        }
        return Result.success(transfer());
    }

    public Message transfer() {
        Message message = new Message();
        message.setId(0);
        message.setMessageType(messageType);
        message.setMessageTitle(messageTitle);
        message.setMessageContent(messageContent);
        message.setMessageUrl(messageUrl);
        message.setCreateTime(new Date());
        message.setFlag(0);
        message.setUserId(userId);
        return message;
    }
}
